///<reference path="../../app/plugin/wechat/wechat.d.ts"/>
import {ChangeDetectorRef, Component} from '@angular/core';
import {ActionSheetController, AlertController, NavController, NavParams, Platform} from 'ionic-angular';
import {JidihttpProvider} from "../../providers/jidihttp/jidihttp";
import {HttpClient} from "@angular/common/http";
import {Camera, CameraOptions} from "@ionic-native/camera";
import {ImagePicker, ImagePickerOptions} from "@ionic-native/image-picker";

/**
 * Generated class for the MyuploadPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-myupload',
  templateUrl: 'myupload.html',
})
export class MyuploadPage {
  avatar: string = "";
  /** 活动 **/
  huodongMovies: Array<any>=[];
  /** 活动 基地 **/
  tasklisthuodong:Array<any>=[];//list列表
  pageno:number=1;
  constructor(public alertCtrl: AlertController,public imagePicker: ImagePicker, public camera: Camera,public platform: Platform,
              public actionsheetCtrl: ActionSheetController,public jidiProvider:JidihttpProvider,public cdchange:ChangeDetectorRef,public navCtrl: NavController, public navParams: NavParams,public http:HttpClient) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MyuploadPage');
    this.huodonglist();
    /** weixin config **/
    this.weixinconfig();
  }
  /** 活动 http **/
  huodonglist(){
    this.jidiProvider.getHuodongHttp(this.pageno).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.huodongMovies.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.tasklisthuodong=this.huodongMovies;
        this.cdchange.detectChanges();
      }
    },error => {

    });
  }


  openMenu(){
    let actionSheet = this.actionsheetCtrl.create({
      title: '请选择操作',
      cssClass: 'action-sheets-basic-page',
      buttons: [
        {
          text: '拍照',
          icon: !this.platform.is('ios') ? 'arrow-dropright-circle' : null,
          handler: () => {
            this.takePhoto();
          }
        },
        {
          text: '从相册选择',
          icon: !this.platform.is('ios') ? 'heart-outline' : null,
          handler: () => {
            this.chooseFromAlbum();
          }
        },
        {
          text: '取消',
          role: 'cancel', // will always sort to be on the bottom
          icon: !this.platform.is('ios') ? 'close' : null,
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });
    actionSheet.present();
  }

  takePhoto() {
    const options: CameraOptions = {
      quality: 100,
      sourceType: this.camera.PictureSourceType.CAMERA,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    };

    this.camera.getPicture(options).then((imageData) => {

    }, (err) => {
      console.log('获取图片失败');
    });
  }


  chooseFromAlbum() {
    const options: ImagePickerOptions = {
      maximumImagesCount: 6,
      width: 200,
      height: 200
    };
    this.imagePicker.getPictures(options).then(images => {
      if (images.length > 6) {
        this.presentAlert();
      } else if (images.length === 1) {
        console.log('Image URI: ' + images[0]);
        this.avatar = images[0].slice(7);
      }
    }, error => {
      console.log('Error: ' + error);
    });
  }
  presentAlert() {
    let alert = this.alertCtrl.create({title: "上传失败", message: "最多只能选择6张图片", buttons: ["确定"]});
    alert.present().then(value => {
      return value;
    });
  }

  /** weixin **/
  weixinconfig(){

  }

  /** xuanzexiangji **/



}
